package com.qhgrain.api.menurolepermission.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.paas.base.modules.utils.lang.StringUtil;
import com.qhgrain.api.menurolepermission.dao.MenuMapper;
import com.qhgrain.api.menurolepermission.entity.Menu;
import com.qhgrain.api.menurolepermission.service.IMenuService;
@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl implements IMenuService{

	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public void insert(Menu menu) {
		
		this.menuMapper.insertSelective(menu);
	}

	@Override
	public void delete(List<String> ids) {
		Example e = new Example(Menu.class);
		e.createCriteria().andIn("id", ids);
		this.menuMapper.deleteByExample(e);
	}

	@Override
	public void update(Menu menu) {
		this.menuMapper.updateByPrimaryKey(menu);
		
	}

	public void updateStatus(List<String> menuId,String status){
		this.menuMapper.updateStatus(menuId, status);
	}
	@Override
	public List<Menu> queryAll(String appId,String userId,String status) {
		if(StringUtil.isBlank(userId) || "null".equalsIgnoreCase(userId)){
			Example e = new Example(Menu.class);
			Criteria c = e.createCriteria();
			c.andEqualTo("appId",appId);
			if(StringUtil.isNotBlank(status) && !"null".equalsIgnoreCase(status)){
				c.andEqualTo("status", status);
			}
			e.orderBy("levelNum").asc().orderBy("orderNum").asc();
			return this.menuMapper.selectByExample(e);
		}else{
			return this.menuMapper.queryAll(appId, userId, status);
		}
		
	}

	@Override
	public Menu find(String menuId) {
		
		return this.menuMapper.selectByPrimaryKey(menuId);
	}

	@Override
	public List<Menu> queryNextChildMenu(String parentMenuId,String userId,String status) {
		
		if(StringUtil.isBlank(userId) || "null".equalsIgnoreCase(userId)){
			Example e = new Example(Menu.class);
			Criteria c = e.createCriteria();
			c.andEqualTo("parentMenuId",parentMenuId);
			if(StringUtil.isNotBlank(status) && !"null".equalsIgnoreCase(status)){
				c.andEqualTo("status", status);
			}
			e.orderBy("levelNum").asc().orderBy("orderNum").asc();
			return this.menuMapper.selectByExample(e);
		}else{
			return this.menuMapper.queryNextChildMenu(parentMenuId, userId, status);
		}
	}

	@Override
	public List<Menu> queryAllChildMenu(String menuId, String userId,String status) {
		menuId = "-"+menuId+"-";
		if(StringUtil.isBlank(userId) || "null".equalsIgnoreCase(userId)){
			Example e = new Example(Menu.class);
			Criteria c = e.createCriteria();
			c.andLike("path","%"+ menuId+"%");
			if(StringUtil.isNotBlank(status) && !"null".equalsIgnoreCase(status)){
				c.andEqualTo("status", status);
			}
			e.orderBy("levelNum").asc().orderBy("orderNum").asc();
			return this.menuMapper.selectByExample(e);
		}else{
			return this.menuMapper.queryAllChildMenu(menuId, userId, status);
		}
	}
	public List<Menu> queryTopLevelMenu(String appId,String userId,String status){
		if(StringUtil.isBlank(userId)){
			Example e = new Example(Menu.class);
			Criteria c = e.createCriteria();
			c.andEqualTo("appId",appId).andIsNull("parentMenuId");
			if(StringUtil.isNotBlank(status)){
				c.andEqualTo("status",status);
			}
			e.orderBy("levelNum").asc().orderBy("orderNum").asc();
			return this.menuMapper.selectByExample(e);
		}else{
			return this.menuMapper.queryTopLevelMenu(appId, userId, status);
		}
		
	}

	@Override
	public List<Menu> queryParentMenus(String menuId) {
		List<Menu> list = new ArrayList<Menu>();
		Menu m = this.menuMapper.selectByPrimaryKey(menuId);
		if(m != null && StringUtil.isNotBlank(m.getParentMenuId())){
			int i=1;
			String pId = m.getParentMenuId();
			Menu p = null;
			while(StringUtil.isNotBlank(pId)){
				p = this.menuMapper.selectByPrimaryKey(pId);
				if(p !=null){
					pId = p.getParentMenuId();
					list.add(p);
				}else{
					break;
				}
				i++;
				if(i>10){
					break;
				}
			}
		}
		Collections.reverse(list);
		return  list;
	}

	
}
